import { config } from "../config/config";
import { cache, promisic } from "./util";

class Http {
  static async request({ url, data = {}, method = 'GET' }) {
    let res = await promisic(wx.request)({
      url: `${config.apiBaseUrl}${url}`,
      data: data,
      method: method,
      header: {
        token: wx.getStorageSync('token'),
      },
    });
    return res.data; // res 是整个包, 包括 cookie, header, body => data, 应该把body返回, 也就是 res.data
  }

  static async get_code() {
    return (await promisic(wx.login)({})).code;
  }

  static async login() {
    let code = await Http.get_code();
    let results = (await Http.request({
      url: 'wechat/index/login',
      data: { code: code },
    }));
    // console.log(results)
    let token = results.data.token
    //  wx.setStorageSync('token', token)
    cache('token', token);
    return token;
  }

}
export { Http }